require "vector"
require "std"
require "d20"

function d12()
  local object={}
  -- every object needs a mass, angular mass, position, angular velocity, etc, etc.
  object.position=Vector{0,0,0}
  object.mass=1
  object.velocity=Vector{0,0,0}
  object.angularMass=4
  object.angularVelocity=Vector{0,0,0}
  object.color=0xFFFF00
  object.radius=0.9
  
  -- we take a d20 and turn every face into a vertex
  object.vertices=table.map(d20().surfaces, func.pbind2(__index,"center"))
  --table.transform(object.vertices, Vector)
  
  --we also rescale because it's too big
  table.transform(object.vertices, function(v) v[1],v[2],v[3]=unpack(v*1.1) return v end)
  
  
  -- every object has a set of surfaces that hold the polygons and the textures
  local polygons=verticesToPolygons(object.vertices)
  object.surfaces=table.transform(polygons,polygonToSurface)
  
  --every surface also has a texture that contains the number image.
  for i,surface in ipairs(object.surfaces) do
    surface.texture={
      image=i..".png",
      o=surface.center,
      y=math.avg(surface.polygon[3], surface.polygon[2], surface.center)
    }
    surface.texture.x=vector.cross(surface.normal, (surface.texture.y-surface.texture.o))+surface.texture.o
  end
  
  -- every object also has a set of vectors that need to be updated with rotation.
  object.allVertices=extractAllVertices(object)

  -- also surfaces refer back to their parent object
  for i,surface in ipairs(object.surfaces) do 
    surface.object=object
  end
  
return object end
